// JavaScript Document
define(function (require, exports, module) {
	//var ListItem=require("./module/ListItem");
	var ListPage=require("ListPage");
	
	SelectorPage = function(){
		//ListPage.call(this)
		var self=this;
		//alert(arguments.length);
		$("#allSelectButton").unbind("click")
		$("#allSelectButton").bind("click",function(event){self.allSelectButtonClickHandle(event)})
		
		$("#allRemoveButton").unbind("click")
		$("#allRemoveButton").bind("click",function(event){self.allRemoveButtonClickHandle(event)})
		
		
		//后退前进，如已选元素为空时清空selectedIds值 以及所有selectedIds与下面数量不一致的解决
		var total=$("#selectedListModule table tbody tr").length;
		if(total==0){
			document.getElementById("selectedIds").value=""
		}
	};
	SelectorPage.prototype=new ListPage();
	SelectorPage.prototype.selectedListItemMode=""
	SelectorPage.prototype.getValue=function(){
		this.selectedIdsAppendInputModule.getValue();
	}
		
	SelectorPage.prototype.allSelectButtonClickHandle=function(event){
		var listItems=this.list.listItems;
		//getStatus
		var total=listItems.length;
		var enableTotal=0
		for(var i=0;i<listItems.length;i++){
			if(listItems[i].getSelectButtonIsVisible()){
				enableTotal++;
			}
		}
		if(enableTotal==0){
			alert("没有可选择的记录！");
			return false;
		}
		if(confirm("是否确认选择本页"+enableTotal+"条记录?")){
			this.allSelect();
			return true;
		}else{
			return false;
		}
	}
	SelectorPage.prototype.allSelect=function(){
		var listItems=this.list.listItems
		var total=listItems.length;
		for(var i=0;i<total;i++){
			var isVisible=$(".select .button",listItems[i].element).is(":visible")
			if(isVisible){
				this.appendById(listItems[i].getId())
			}
		}
	}
	
	SelectorPage.prototype.allRemoveButtonClickHandle=function(event){
		this.allRemove();
	}
	SelectorPage.prototype.allRemove=function(){
		var listItems=this.selectedList.listItems
		var total=listItems.length;
		for(var i=0;i<total;i++){
			var isVisible=$(".select .button",listItems[i].element).is(":visible")
			//if(isVisible){
				this.removeById(listItems[i].getId())
			//}
		}
	}
	
	SelectorPage.prototype.updateAllButton=function(){
		this.updateAllRemoveButton();
		this.updateAllSelectButton();
	}
	
	SelectorPage.prototype.updateAllSelectButton=function(){
		var valueArray=this.selectedIdsAppendInputModule.getValueArray()
		var listValueArray=this.list.getIdArray();
		
		var selectedLength=0;
		for(var i=0;i<valueArray.length;i++){
			for(var j=0;j<listValueArray.length;j++){
				if(valueArray[i]==listValueArray[j]){
					selectedLength++;
				}
			}
		}
		if(selectedLength==listValueArray.length){
			$("#allSelectButton").attr("disabled","disabled")
		}else{
			$("#allSelectButton").removeAttr("disabled")
		}
	}
	
	SelectorPage.prototype.updateAllRemoveButton=function(){
		var valueArray=this.selectedIdsAppendInputModule.getValueArray()
		if(valueArray.length==0){
			$("#allRemoveButton").attr("disabled","disabled")
		}else{
			$("#allRemoveButton").removeAttr("disabled")
		}
	}
	
	
	SelectorPage.prototype.appendById=function(id){
		var item=this.list.getItemById(id)
		this.list.selectById(id);
		var selectItem=this.selectedList.getItemById(id)
		
		if(selectItem==null){
			var element=$(item.element).clone().get(0);
			var listItem=new this.ListItem(element,0);
			listItem.setMode(this.selectedListItemMode)
			this.selectedList.append(listItem);
			this.selectedIdsAppendInputModule.append(item.getId())
		}
	}
	SelectorPage.prototype.removeItem=null;
	SelectorPage.prototype.removeById=function(id){
		this.removeItem=this.selectedList.getListItemById(id);
		this.list.deselectById(id);
		this.selectedList.removeById(id);
		this.selectedIdsAppendInputModule.remove(id)
	}
	SelectorPage.prototype.filterSelected=function(){
		var id="";
		for(var i=0;i<this.selectedList.listItems.length;i++){
			id=this.selectedList.listItems[i].getId();
			this.list.selectById(id);
			this.selectedList.selectById(id);
		}
	}
	SelectorPage.prototype.getListItemById=function(id){
		var item=this.list.getItemById(id);
		if(item==null){
			item=this.removeItem;
		}
		return item;
	}
	SelectorPage.prototype.getSelectedListItemById=function(id){
		var item=this.selectedList.getItemById(id);
		return item;
	}
	window.getSelectedIds=function(){
		var selectedIds=document.getElementById("selectedIds").value
		return selectedIds;
	}
	window.getValue=function(){
		return window.getSelectedIds();
	}
	module.exports = SelectorPage;
});